package leetcode_day._2022._202208._2131;

import general_class.TreeNode;

/**
 * 998. 最大二叉树 II
 * 算法：模拟
 * root 是由 nums 构成的最大数,
 * 将 val 加到 nums 末尾后, root 还是一颗最大数
 *
 * @author yzh
 * @version 1.0
 * @date 2022/8/30 21:53
 */
public class _30 {

    public TreeNode insertIntoMaxTree(TreeNode root, int val) {
        TreeNode node = new TreeNode(val);
        TreeNode prev = null, cur = root;
        while (cur != null && cur.val > val) {
            prev = cur;
            cur = cur.right;
        }
        if (prev == null) {
            node.left = cur;
            return node;
        } else {
            prev.right = node;
            node.left = cur;
            return root;
        }
    }

}
